package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.Spufeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private Spufeign spufeign;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    public static final String CART = "CART_";

    @Override
    public void add(String skuID, Integer num, String username) {


        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuID);
        if (orderItem == null) {
            orderItem = new OrderItem();
            Result<Sku> byId = skuFeign.findById(skuID);
            Sku sku = byId.getData();
            Result<Spu> resultspu = spufeign.findById(sku.getSpuId());
            Spu spu = resultspu.getData();
            orderItem = this.sku2OrderItem(sku, spu, num);
        } else {
            orderItem.setNum(num);
            if (orderItem.getNum()<=0){
                redisTemplate.boundHashOps(CART+username).delete(skuID);
            return;
            }
            orderItem.setMoney(num*orderItem.getPrice());
            orderItem.setPayMoney(num*orderItem.getPrice());
        }

        redisTemplate.boundHashOps(CART+username).put(skuID,orderItem);

    }

    @Override
    public Map list(String username) {
        Map map=new HashMap();
        List <OrderItem> orderItemlist= redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList",orderItemlist);
        Integer totalNum=0;
        Integer totalPrice=0;
        for (OrderItem orderItem : orderItemlist) {
            totalNum+=orderItem.getNum();
            totalPrice+=orderItem.getPayMoney();
        }
        map.put("totalNum",totalNum);
        map.put("totalPrice",totalPrice);
        return map;

    }

    //sku转换为orderItem
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num * orderItem.getPrice()); //单价*数量
        orderItem.setPayMoney(num * orderItem.getPrice()); //实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num); //重量=单个重量*数量
        //分类ID设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
